<template>
  <el-dialog v-model="dialogInfo.visible" :title="dialogInfo.title" :width="dialogInfo.width" align-center>
    <el-form ref="formRef" label-position="left" :model="formModel" :rules="formRules" label-width="120px">
      <el-row>
        <el-col :sm="24">
          <el-form-item label="大楼" prop="buildingId">
            <el-select clearable class="w100" v-model="formModel.buildingId" placeholder="请选择大楼" autocomplete="off" @change="floorFetch">
              <el-option v-for="item in buildingStore.bildingList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :sm="24">
          <el-form-item label="楼层" prop="floorId">
            <el-select clearable class="w100" v-model="formModel.floorId" placeholder="请选择楼层" autocomplete="off">
              <el-option v-for="item in floorStore.floorList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :sm="24">
          <el-form-item label="教室名称" prop="name">
            <el-input clearable class="w100" v-model.trim="formModel.name" autocomplete="off" placeholder="请输入教室名称"></el-input>
          </el-form-item>
        </el-col>

        <el-col :sm="24">
          <el-form-item label="教室ip" prop="ip">
            <el-input clearable class="w100" v-model.trim="formModel.ip" autocomplete="off" placeholder="请输入教室ip"></el-input>
          </el-form-item>
        </el-col>

        <el-col :sm="24">
          <el-form-item label="教室类型" prop="roomType">
            <el-select clearable class="w100" v-model="formModel.roomType" placeholder="请选择教室类型" autocomplete="off">
              <el-option v-for="item in roomTypeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="info" plain @click="onCancel()">取消</el-button>
        <el-button type="primary" @click="onSubmit()">提交</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
/**
 * 管理员编辑组件
 */
import { ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import EsAjax from '@/utils/EsAjax';
import ApiUrls from '@/apis/index';
import { getNewObj } from '@/utils/EsCommon';
import { roomParams, roomTypeList } from '@/apis/model/RoomModel';
import useBuildingStore from '@/stores/useBuildingStore';
import useFloorStore from '@/stores/useFloorStore';

// ======================================================
//                      变量声明
// ======================================================
const buildingStore = useBuildingStore();
const floorStore = useFloorStore();

// emit
const emit = defineEmits(['success']);
// 表单数据
const formAttrs = getNewObj(roomParams());
const roomTypeOption = ref(roomTypeList);

const formModel = ref(Object.assign({}, formAttrs));
// 弹窗信息
const dialogInfo = ref({
  visible: false,
  title: '',
  width: '600px',
})
// 表单组件的引用实例
const formRef = ref(null);

// 表单校验规则
const formRules = ref({
  floorId: [{ required: true, trigger: "blur", message: "请选择楼层" }],
  buildingId: [{ required: true, trigger: "blur", message: "请选择大楼" }],
  name: [{ required: true, trigger: "blur", message: "请输入教室名称" }],
  roomType: [{ required: true, trigger: "blur", message: "请输入教室类型" }],
  ip: [{ required: true, trigger: "blur", message: "请输入教室ip" }],

})

// ======================================================
//                      点击操作
// ======================================================

// 点击提交按钮
const onSubmit = () => {
  formRef.value.validate((valid, fields) => {
    if (!valid) {
      console.log('表单校验失败：', fields);
      return;
    }

    let params = {
      name: formModel.value.name,
      floorId: Number(formModel.value.floorId),
      buildingId: Number(formModel.value.buildingId),
      roomType: Number(formModel.value.roomType),
      ip: formModel.value.ip,
    }
    if (formModel.value.id > 0) {
      params.id = formModel.value.id
    }


    EsAjax.post(ApiUrls.roomEdit, params).then(res => {
      if (res.data.code != 200) {
        ElMessage.warning(res.data.data+res.data.msg);
        return;
      }
      dialogInfo.value.visible = false;
      ElMessage.success('提交成功');
      emit('success');
    }).catch(err => {
      ElMessage.error('网络错误：' + err.message);
    })
  })
}

// 点击取消按钮
const onCancel = () => {
  formModel.value = Object.assign({}, formAttrs);
  formRef.value.resetFields();
  dialogInfo.value.visible = false;
}
// ======================================================
//                      处理事件
// ======================================================

// 加载数据
const show = async (id, title, item = null, width = '600px') => {
  dialogInfo.value.title = title;
  dialogInfo.value.width = width;
  dialogInfo.value.visible = true;
  formModel.value = Object.assign({}, formAttrs)
  // 加载详情数据
  if (id) {
    formModel.value = Object.assign({ ...formAttrs }, getNewObj(item));

    if (`${formModel.value.roomType}`) {
      formModel.value.roomType = String(formModel.value.roomType)
    }
    floorStore.getFloorList(formModel.value.buildingId)
  }

  // 加载大楼数据
  buildingStore.getBuildList()
}
// 加载楼层数据
const floorFetch = (e) => {
  floorStore.getFloorList(e)
}
defineExpose({
  show
})
</script>

<style scoped>
</style>
